package com.lyz.myself.controller;

import com.lyz.myself.config.utils.Result;
import com.lyz.myself.config.utils.base.CodeMsg;
import com.lyz.myself.pojo.Po.Log;
import com.lyz.myself.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: myself
 * @Date: 2019/3/1 9:33
 * @Author: Mr.Deng
 * @Description:
 */
@Api(description="日志")
@RestController
@RequestMapping("/log")
public class LogController {

    @Autowired
    LogService logService;

    @ApiOperation("添加日志")
    @RequiresAuthentication//登陆状态
    @RequestMapping(value = "/insertLog", method = RequestMethod.POST)
    public Result    insertSelective(@RequestBody Log log) {
        if(log!=null){
          int insertLog = logService.insertSelective(log);
            if (insertLog > 0) {
                return Result.success("添加成功！");
            } else {
                return Result.error(CodeMsg.ERROR, "添加失败！");
            }
        }else{
            return Result.error(CodeMsg.ERROR, "请输入要添加的沟通日志信息！");
        }

    }


    @ApiOperation("删除日志")
    @RequiresAuthentication//登陆状态
    @RequestMapping(value = "/deleteByPrimaryKey", method = RequestMethod.POST)
    public Result deleteByPrimaryKey(@RequestParam String log_id) {
        if(log_id!=null){
            List<Integer> list = new ArrayList<Integer>();
            String[] str = log_id.split(",");
            for (int i = 0; i < str.length; i++) {
                list.add(Integer.parseInt(str[i]));
            }
            int insertLog = logService.deleteByPrimaryKey(list);
            if (insertLog > 0) {
                return Result.success("删除成功！");
            } else {
                return Result.error(CodeMsg.ERROR, "删除失败！");
            }
        }else{
            return Result.error(CodeMsg.ERROR, "请选择要删除的日志信息！");
        }

    }


    @ApiOperation("修改日志")
    @RequiresAuthentication//登陆状态
    @RequestMapping(value = "/updateByPrimaryKeySelective", method = RequestMethod.POST)
    public Result updateByPrimaryKeySelective(@RequestBody Log log) {
        if(log!=null){
            int insertLog = logService.updateByPrimaryKeySelective(log);
            if (insertLog > 0) {
                return Result.success("修改成功！");
            } else {
                return Result.error(CodeMsg.ERROR, "修改失败！");
            }
        }else{
            return Result.error(CodeMsg.ERROR, "请输入要修改的日志信息！");
        }

    }


}
